<!-- subpkg_consult/payment/index.vue -->
<script lang="ts" setup>
  import { createOrderApi, orderPayApi, preOrderApi } from '@/apis/order'
  import { patientDetailApi } from '@/apis/patient'
  import { useConsultStore } from '@/stores/consult'
  import type { OrderParams, OrderPreData, OrderPreParams, PayParams } from '@/types/order'
  import type { PatientItem, PatientParams } from '@/types/patient'
  import { ref } from 'vue'

  const store = useConsultStore()
  const preOrderInfo = ref<OrderPreData>({} as OrderPreData)
  //#region  1 调用接口 获取预付订单信息 金额
  const getPreOrder = async () => {
    const params: OrderPreParams = {
      illnessType: store.illnessType,
      type: store.type,
    }
    const res = await preOrderApi(params)
    preOrderInfo.value = res
  }
  getPreOrder()
  //#endregion

  //#region 2 在线获取患者信息
  const patientDetail = ref({} as PatientItem)
  const getpatientDetail = async () => {
    const res = await patientDetailApi(store.patientId)
    console.log('res', res)

    patientDetail.value = res as PatientItem
  }
  getpatientDetail()
  //#endregion

  //#region 立即支付
  const customPaymentRef = ref()
  const onPayNow = () => {
    customPaymentRef.value.open()
  }

  // 子组件点击立即支付支付流程
  const onChangePayChannelIndex = async (index: number) => {
    // console.log(index)
    if (index === 0) {
      uni.showToast({ title: '暂时不支持微信支付', icon: 'none' })
    }

    //创建订单
    const orderParams: OrderParams = {
      // 1  找医生/极速问诊-是否就诊过0未就诊1就诊过
      consultFlag: store.illnessInfo.consultFlag,
      // 2 选择的科室id-极速问诊必填(选择的科室)
      depId: store.depId,
      // 3 病情--症状描述
      illnessDesc: store.illnessInfo.illnessDesc,
      // 4 找医生/极速问诊-患病时间1一周内2一月内3半年内4半年以上
      illnessTime: store.illnessInfo.illnessTime,
      // 5  极速问诊类型：0普通1三甲
      illnessType: store.illnessType,
      // 6 者id,用于关联患者信息
      patientId: store.patientId,
      // 7 补充病例信息-图片集合
      pictures: store.illnessInfo.pictures.map((v) => ({ id: v.id, url: v.url })),
      // 8 就诊类型1找医生 2极速问诊 3开药问诊默认是
      type: store.type,
    }
    const resOrder = await createOrderApi(orderParams)

    //清空数据
    store.clear()
    // console.log(resOrder)

    //调用支付接口
    const payParams: PayParams = {
      orderId: resOrder.id,
      payCallback: 'http://localhost:5173/#/subpkg_consult/room/index',
      paymentMethod: index.toString(),
    }
    // 调用支付接口成功（返回的是一个支付宝支付的网页URL）
    const resPay = await orderPayApi(payParams)
    location.href = resPay.payUrl
  }
  //#endregion
</script>

<template>
  <scroll-page>
    <view class="payment-page">
      <uni-section
        title-font-size="32rpx"
        title-color="#121826"
        padding="30rpx"
        :title="`图文问诊 ${preOrderInfo.payment}元`"
      >
        <uni-list :border="false">
          <uni-list-item
            title="极速问诊"
            note="自动分配医生"
            thumb="/static/uploads/doctor-avatar.jpg"
            thumb-size="lg"
          />
          <uni-list-item
            title="优惠券"
            show-arrow
            :right-text="`-¥${preOrderInfo.couponDeduction}`"
          />
          <uni-list-item title="积分抵扣">
            <template #footer>
              <view class="uni-list-text-red"> -¥{{ preOrderInfo.pointDeduction }}</view>
            </template>
          </uni-list-item>
          <uni-list-item title="实付款">
            <template #footer>
              <view class="uni-list-text-red">¥{{ preOrderInfo.actualPayment }}</view>
            </template>
          </uni-list-item>
        </uni-list>
      </uni-section>

      <view class="dividing-line"></view>

      <uni-section title-font-size="32rpx" title-color="#121826" padding="30rpx" title="患者资料">
        <uni-list :border="false">
          <uni-list-item title="患者信息">
            <template #footer>
              <view class="uni-list-text-gray">
                {{ patientDetail.name }} | {{ patientDetail.genderValue }} |
                {{ patientDetail.age }}岁
              </view>
            </template>
          </uni-list-item>
          <uni-list-item border title="病情描述" note="头痛,头晕,恶心" />
        </uni-list>
      </uni-section>

      <!-- <view class="payment-agreement">
        <radio color="#20c6b2" value="1" />
        我已同意<text style="color: #20c6b2">支付协议</text>
      </view> -->
    </view>
    <!-- 下一步操作 -->
    <view class="next-step">
      <view class="total-amount">
        合计: <text class="number">¥{{ preOrderInfo.actualPayment }}</text>
      </view>
      <button class="uni-button" @click="onPayNow">立即支付</button>
    </view>
    <custom-payment
      ref="customPaymentRef"
      :actualPayment="preOrderInfo.actualPayment"
      @change-pay-channelIndex="onChangePayChannelIndex"
    ></custom-payment>
  </scroll-page>
</template>

<style lang="scss">
  @import './index.scss';
</style>
